<?php
class AlipayController extends AlipayAppController {
	
	var $name = 'Alipay';
	var $uses = array();
	
	function beforeFilter(){
		$this->Auth->allow('pay','payback','alireturn');
		parent::beforeFilter();
	}
	
	function admin_index(){
		
	}
	
	/**
	 * 代理充值
	 * @author zmx@gootop.net  
	 * @Date 2011-12-24
	 * @return 
	 */
	function pay($order_id = false){
		$this->layout = false;
		$this->loadModel('UserOrder');
		$userOrder = $this->UserOrder->findById($order_id);
		$order_no = $userOrder['UserOrder']['id']; 			//订单 号
		$ali_order = '秦文票务票款';						//订单名称
		$ali_money = $userOrder['UserOrder']['price_total'];							//订单金额
		$pay_bank = 'directPay';					//支付方式
		
		App::import('Vendor', 'Alipay.Alipay', array('file' => 'GTPay.php'));
		App::import('Vendor', 'Alipay.AlipayService', array('file' => 'AlipayService.php'));
		//回调路径
		$returnUrl = Router::url(array('admin'=>false,'plugin'=>'alipay','controller'=>'Alipay','action'=>'payback','return'),true);
		//通知路径
		$notifyUrl = Router::url(array('admin'=>false,'plugin'=>'alipay','controller'=>'Alipay','action'=>'payback','notify'),true);
		
		$gtp = new GTPay($returnUrl,$notifyUrl);
		$gtp->setAlipayParam($order_no,$ali_order,'',$ali_money);
		
		//构造请求函数
		$alipay = new AlipayService($gtp->getAlipayParam(),$gtp->getKey(),$gtp->getSignType(),'');
		$sHtmlText = $alipay->build_form();
		$this->set('aliparam',$sHtmlText);
	}
	
	function payback($type='notify'){
		
		App::import('Vendor', 'Alipay.Alipay', array('file' => 'GTPay.php'));
		App::import('Vendor', 'Alipay.AlipayNotify', array('file' => 'AlipayNotify.php'));
		$gtp = new GTPay();
		$alipay = new AlipayNotify($gtp->getPartner(),$gtp->getKey(),$gtp->getSignType(),$gtp->getInputCharset(),$gtp->getTransport());    //构造通知函数信息
		if($type == 'notify'){
			$this->autoRender = false;
			$verify_result = $alipay->notify_verify();  //计算得出通知验证结果
		}else{
			$verify_result = $alipay->return_verify();
		}
		if($verify_result) {
			
		    $dingdan           = $_REQUEST['out_trade_no'];			//获取支付宝传递过来的订单号
		    $total             = $_REQUEST['total_fee'];			//获取支付宝传递过来的总价格
			
		    if($_REQUEST['trade_status'] == 'TRADE_FINISHED' ||$_REQUEST['trade_status'] == 'TRADE_SUCCESS') {    //交易成功结束
				//TODO处理逻辑
		    	$this->loadModel('UserOrder');
		    	if($this->UserOrder->updateOrderState($dingdan,2)){
			    	$res = true;
		    	}else{
		    		$res = false;
		    	}
		    	if($res){	
			    	//正尝执行
				    if($type == 'notify'){
						echo "success";		//请不要修改或删除
					}else{
						$this->redirect(array('controller'=>'Alipay','action'=>'alireturn','success'));
					}
		    	}else{
		    		//已执行回调操作
			    	if($type == 'notify'){
						echo "success";		//请不要修改或删除
					}else{
						$this->redirect(array('controller'=>'Alipay','action'=>'alireturn','success_already'));
					}	
		    	}
			     
		    } else {
		    	if($type == 'notify'){
					echo "success";		//请不要修改或删除
				}else{
		    		$this->redirect(array('controller'=>'Alipay','action'=>'alireturn','faild1'));
		    	}
		    }
		}else {
			if($type == 'notify'){
				echo "fail"; //请不要修改或删除
			}else{
				$this->redirect(array('controller'=>'Alipay','action'=>'alireturn','faild2'));
			}
		    
		}
	}
	
	function alireturn($msg = ''){
		if($msg == 'success'){
			$message = '您的订单支付成功';
		}else if($msg == 'success_already'){
			$message = '您的订单支付成功';
		}else{
			$message = '您的订单支付失败';
		}
		$this->set('message',$message);
	}
}
?>